package com.hkbigdata.transform;

import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author liuanbo
 * @creat 2023-03-18-13:29
 * @see 2194550857@qq.com
 */
public class Flink04_TransFrom_KeyBy_Lambda {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        KeyedStream<Integer, String> keySelect = env.fromElements(1, 3, 5, 7, 2, 4, 6, 8, 5)
                .keyBy(data -> data % 2 == 0 ? "偶数" : "奇数");

        keySelect.reduce(new ReduceFunction<Integer>() {
            @Override
            public Integer reduce(Integer value1, Integer value2) throws Exception {

                return value1+value2;
            }
        }).print();

        env.execute();
    }
}
